home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / DistUpgrade / DistUpgradeView.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  12.0 KB  |  299 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from DistUpgradeGettext import gettext as _
  5. from DistUpgradeGettext import ngettext
  6. import subprocess
  7. from subprocess import Popen, PIPE
  8. import apt
  9. import os
  10. import apt_pkg
  11. import signal
  12. import glob
  13. from DistUpgradeAufs import doAufsChroot, doAufsChrootRsync
  14. from DistUpgradeApport import *
  15.  
  16. def FuzzyTimeToStr(sec):
  17.     ''' return the time a bit fuzzy (no seconds if time > 60 secs '''
  18.     sec = int(sec)
  19.     days = sec / 86400
  20.     hours = sec / 3600 % 24
  21.     minutes = sec / 60 % 60
  22.     seconds = sec % 60
  23.     if seconds == 0:
  24.         seconds = 1
  25.     
  26.     map = {
  27.         'str_days': '',
  28.         'str_hours': '',
  29.         'str_minutes': '',
  30.         'str_seconds': '' }
  31.     if days > 0:
  32.         map['str_days'] = ngettext('%li day', '%li days', days) % days
  33.     
  34.     if hours > 0:
  35.         map['str_hours'] = ngettext('%li hour', '%li hours', hours) % hours
  36.     
  37.     if minutes > 0:
  38.         map['str_minutes'] = ngettext('%li minute', '%li minutes', minutes) % minutes
  39.     
  40.     map['str_seconds'] = ngettext('%li second', '%li seconds', seconds) % seconds
  41.     if days > 0:
  42.         return _('%(str_days)s %(str_hours)s') % map
  43.     if hours > 3:
  44.         return map['str_hours']
  45.     if hours > 0:
  46.         return _('%(str_hours)s %(str_minutes)s') % map
  47.     if minutes > 0:
  48.         return map['str_minutes']
  49.     return map['str_seconds']
  50.  
  51.  
  52. class FetchProgress(apt.progress.FetchProgress):
  53.     
  54.     def __init__(self):
  55.         apt.progress.FetchProgress.__init__(self)
  56.         self.est_speed = 0
  57.  
  58.     
  59.     def pulse(self):
  60.         apt.progress.FetchProgress.pulse(self)
  61.         if self.currentCPS > self.est_speed:
  62.             self.est_speed = (self.est_speed + self.currentCPS) / 2
  63.         
  64.         return True
  65.  
  66.     
  67.     def estimatedDownloadTime(self, requiredDownload):
  68.         ''' get the estimated download time '''
  69.         if self.est_speed == 0:
  70.             timeModem = requiredDownload / 57344 / 8
  71.             timeDSL = requiredDownload / 1048576 / 8
  72.             s = _('This download will take about %s with a 1Mbit DSL connection and about %s with a 56k modem.') % (FuzzyTimeToStr(timeDSL), FuzzyTimeToStr(timeModem))
  73.             return s
  74.         s = _('This download will take about %s with your connection. ') % FuzzyTimeToStr(requiredDownload / self.est_speed)
  75.         return s
  76.  
  77.  
  78.  
  79. class InstallProgress(apt.progress.InstallProgress):
  80.     ''' Base class for InstallProgress that supports some fancy
  81.       stuff like apport integration
  82.   '''
  83.     
  84.     def __init__(self):
  85.         apt.progress.InstallProgress.__init__(self)
  86.         self.master_fd = None
  87.  
  88.     
  89.     def run(self, pm):
  90.         pid = self.fork()
  91.         if pid == 0:
  92.             if 'RELEASE_UPGRADE_USE_AUFS_CHROOT' in os.environ:
  93.                 if not doAufsChroot(os.environ['RELEASE_UPGRADE_AUFS_RWDIR'], os.environ['RELEASE_UPGRADE_USE_AUFS_CHROOT']):
  94.                     print 'ERROR: failed to setup aufs chroot overlay'
  95.                     os._exit(1)
  96.                 
  97.             
  98.             signal.signal(signal.SIGPIPE, signal.SIG_IGN)
  99.             res = pm.DoInstall(self.writefd)
  100.             os._exit(res)
  101.         
  102.         self.child_pid = pid
  103.         res = self.waitChild()
  104.         if res == 0 and 'RELEASE_UPGRADE_RSYNC_AUFS_CHROOT' in os.environ:
  105.             logging.info('doing rsync commit of the update')
  106.             if not doAufsChrootRsync(os.environ['RELEASE_UPGRADE_USE_AUFS_CHROOT']):
  107.                 logging.error('FATAL ERROR: doAufsChrootRsync() returned FALSE')
  108.                 return False
  109.         
  110.         return res
  111.  
  112.     
  113.     def error(self, pkg, errormsg):
  114.         ''' install error from a package '''
  115.         apt.progress.InstallProgress.error(self, pkg, errormsg)
  116.         logging.error("got an error from dpkg for pkg: '%s': '%s'" % (pkg, errormsg))
  117.         if '/' in pkg:
  118.             pkg = os.path.basename(pkg)
  119.         
  120.         if '_' in pkg:
  121.             pkg = pkg.split('_')[0]
  122.         
  123.         apport_pkgfailure(pkg, errormsg)
  124.  
  125.  
  126.  
  127. class DumbTerminal(object):
  128.     
  129.     def call(self, cmd, hidden = False):
  130.         ''' expects a command in the subprocess style (as a list) '''
  131.         import subprocess
  132.         subprocess.call(cmd)
  133.  
  134.  
  135. (STEP_PREPARE, STEP_MODIFY_SOURCES, STEP_FETCH, STEP_INSTALL, STEP_CLEANUP, STEP_REBOOT, STEP_N) = range(1, 8)
  136. (_('Preparing to upgrade'), _('Getting new software channels'), _('Getting new packages'), _('Installing the upgrades'), _('Cleaning up'))
  137.  
  138. class DistUpgradeView(object):
  139.     ''' abstraction for the upgrade view '''
  140.     
  141.     def __init__(self):
  142.         pass
  143.  
  144.     
  145.     def getOpCacheProgress(self):
  146.         ''' return a OpProgress() subclass for the given graphic'''
  147.         return apt.progress.OpProgress()
  148.  
  149.     
  150.     def getFetchProgress(self):
  151.         ''' return a fetch progress object '''
  152.         return FetchProgress()
  153.  
  154.     
  155.     def getInstallProgress(self, cache = None):
  156.         ''' return a install progress object '''
  157.         return InstallProgress()
  158.  
  159.     
  160.     def getTerminal(self):
  161.         return DumbTerminal()
  162.  
  163.     
  164.     def updateStatus(self, msg):
  165.         ''' update the current status of the distUpgrade based
  166.             on the current view
  167.         '''
  168.         pass
  169.  
  170.     
  171.     def abort(self):
  172.         ''' provide a visual feedback that the upgrade was aborted '''
  173.         pass
  174.  
  175.     
  176.     def setStep(self, step):
  177.         ''' we have 6 steps current for a upgrade:
  178.         1. Analyzing the system
  179.         2. Updating repository information
  180.         3. fetch packages
  181.         3. Performing the upgrade
  182.         4. Post upgrade stuff
  183.         5. Complete
  184.         '''
  185.         pass
  186.  
  187.     
  188.     def hideStep(self, step):
  189.         ''' hide a certain step from the GUI '''
  190.         pass
  191.  
  192.     
  193.     def showStep(self, step):
  194.         ''' show a certain step from the GUI '''
  195.         pass
  196.  
  197.     
  198.     def confirmChanges(self, summary, changes, downloadSize, actions = None, removal_bold = True):
  199.         ''' display the list of changed packages (apt.Package) and
  200.             return if the user confirms them
  201.         '''
  202.         self.confirmChangesMessage = ''
  203.         self.toInstall = []
  204.         self.toUpgrade = []
  205.         self.toRemove = []
  206.         self.toDowngrade = []
  207.         for pkg in changes:
  208.             if pkg.markedInstall:
  209.                 self.toInstall.append(pkg.name)
  210.                 continue
  211.             if pkg.markedUpgrade:
  212.                 self.toUpgrade.append(pkg.name)
  213.                 continue
  214.             if pkg.markedDelete:
  215.                 self.toRemove.append(pkg.name)
  216.                 continue
  217.             if pkg.markedDowngrade:
  218.                 self.toDowngrade.append(pkg.name)
  219.                 continue
  220.         
  221.         self.toInstall.sort()
  222.         self.toUpgrade.sort()
  223.         self.toRemove.sort()
  224.         self.toDowngrade.sort()
  225.         if not len(self.toInstall) + len(self.toUpgrade) + len(self.toRemove) + len(self.toDowngrade) == len(changes):
  226.             raise AssertionError
  227.         msg = '\n'
  228.         pkgs_remove = len(self.toRemove)
  229.         pkgs_inst = len(self.toInstall)
  230.         pkgs_upgrade = len(self.toUpgrade)
  231.         if pkgs_remove > 0:
  232.             msg += ngettext('%d package is going to be removed.', '%d packages are going to be removed.', pkgs_remove) % pkgs_remove
  233.             msg += ' '
  234.         
  235.         if pkgs_inst > 0:
  236.             msg += ngettext('%d new package is going to be installed.', '%d new packages are going to be installed.', pkgs_inst) % pkgs_inst
  237.             msg += ' '
  238.         
  239.         if pkgs_upgrade > 0:
  240.             msg += ngettext('%d package is going to be upgraded.', '%d packages are going to be upgraded.', pkgs_upgrade) % pkgs_upgrade
  241.             msg += ' '
  242.         
  243.         if downloadSize > 0:
  244.             msg += _('\n\nYou have to download a total of %s. ') % apt_pkg.SizeToStr(downloadSize)
  245.             msg += self.getFetchProgress().estimatedDownloadTime(downloadSize)
  246.         
  247.         if pkgs_upgrade + pkgs_inst + pkgs_remove > 100:
  248.             msg += '\n\n%s' % _('Fetching and installing the upgrade can take several hours. Once the download has finished, the process cannot be cancelled.')
  249.         
  250.         if pkgs_upgrade + pkgs_inst + pkgs_remove < 1:
  251.             summary = _('Your system is up-to-date')
  252.             msg = _('There are no upgrades available for your system. The upgrade will now be canceled.')
  253.             self.error(summary, msg)
  254.             return False
  255.         self.confirmChangesMessage = msg
  256.         return True
  257.  
  258.     
  259.     def askYesNoQuestion(self, summary, msg, default = 'No'):
  260.         """ ask a Yes/No question and return True on 'Yes' """
  261.         pass
  262.  
  263.     
  264.     def confirmRestart(self):
  265.         ''' generic ask about the restart, can be overridden '''
  266.         summary = _('Reboot required')
  267.         msg = _('The upgrade is finished and a reboot is required. Do you want to do this now?')
  268.         return self.askYesNoQuestion(summary, msg)
  269.  
  270.     
  271.     def error(self, summary, msg, extended_msg = None):
  272.         ''' display a error '''
  273.         pass
  274.  
  275.     
  276.     def information(self, summary, msg, extended_msg = None):
  277.         ''' display a information msg'''
  278.         pass
  279.  
  280.     
  281.     def processEvents(self):
  282.         ''' process gui events (to keep the gui alive during a long
  283.             computation '''
  284.         pass
  285.  
  286.     
  287.     def showDemotions(self, summary, msg, demotions):
  288.         '''
  289.       show demoted packages to the user, default implementation
  290.       is to just show a information dialog
  291.       '''
  292.         self.information(summary, msg, '\n'.join(demotions))
  293.  
  294.  
  295. if __name__ == '__main__':
  296.     fp = FetchProgress()
  297.     fp.pulse()
  298.  
  299.